package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Academy;
import model.Exam;
import model.Subject;
import model.beans.ExamenBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.misc.Util;

public class ExamAction  extends MappingDispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode;
    	String str_subjectCode = request.getParameter("subject_id");
    	if((str_academyCode = request.getParameter("academy_id"))==null){
    		str_academyCode = Academy.academySubject(str_subjectCode);
    	}    	
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("lst_examen", Exam.list(str_academyCode, str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		forma.initialize(mapping);
		forma.reset(mapping, request);
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		return mapping.findForward(Util.DEFAULT);
	} 
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		DynaActionForm forma = (DynaActionForm)form;
		ExamenBean examen = Exam.instance( ((Integer)forma.get("exam_id")).intValue() );
		examen.setAcademy_id(str_academyCode);
		
		forma.set("lugar", examen.getLugar());
		forma.set("fecha", examen.getFecha());
		forma.set("hora", examen.getHora());
		
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		DynaActionForm forma = (DynaActionForm)form;
		if( Exam.delete(((Integer)forma.get("exam_id")).intValue()) > 0 ){
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
    	}else{
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailDelete"));
    	}
		request.setAttribute("lst_examen", Exam.list(str_academyCode, str_subjectCode));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		DynaActionForm forma = (DynaActionForm)form;
		
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	String str_forward;
		if( ((Integer)forma.get("exam_id")).intValue() == 0 ){
			if( model.Exam.add(forma.getMap(), str_subjectCode) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			if( model.Exam.edit(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.EDIT;
			}
		}
		
		request.setAttribute("lst_examen", Exam.list(str_academyCode, str_subjectCode));
		return mapping.findForward(str_forward);
	}
}
